package com.project.filter;

import com.project.bean.RootBean;
import com.project.bean.UserBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BaseFilter implements Filter {
    private String root;
    private String location;
    public BaseFilter(String root, String location){
        this.root=root;
        this.location=location;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       System.out.println("进入过滤器*****&*&（&（*&（*&（*&");


        HttpServletRequest request1=(HttpServletRequest)request;
        HttpServletResponse response1=(HttpServletResponse)response;
        //获取用户
        UserBean user=(UserBean) request1.getSession().getAttribute("loginUser");
//        System.out.println("打印用户&*&*&%%^%^^");
        System.out.println(user);

        String rootName="";

        if(user == null) {
            response1.sendRedirect(location);
            return;
        }

        System.out.println("用户不等于空&*&*&%%^%^^");
        System.out.println(user);
        List<RootBean> rootList=user.getUserRoot();
        System.out.println("打印用户权限");
        System.out.println(rootList);
        for (RootBean r : rootList) {

            System.out.println("-------------root----------"+root);
            System.out.println("-------------rootName----------"+r.getName());
            System.out.println("------------equals-------------"+root.equals(r.getName()));
            if (root.equals(r.getName())) {
                chain.doFilter(request1, response1);
                return;
            }
        }
        if(rootName.length()!=0){
            System.out.println("@@@@@@@@@@@@@@@@@@");
            System.out.println("用户登陆成功");
//            response1.sendRedirect(location);
//            chain.doFilter(request1, response1);
            System.out.println("@@@@@@@@@@@@@@@@@@");
        }else {
            response1.sendRedirect(location);
        }

    }

    @Override
    public String toString() {
        return "BaseFilter{" +
                "root='" + root + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}
